<template>
  <div class="tpl-container">
    <div class="mb20 condition-container">
      <div class="condition">
        <span class="color_494949 ft12 pr5">彩屏编码：</span>
        <ElInput
          v-model="eslCode"
          class="w164 mr14"
          placeholder="请输入模板编码"
        />
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">彩屏名称：</span>
        <ElInput v-model="eslName" class="w164" placeholder="请输入模板名称" />
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">屏幕类型：</span>
        <el-select v-model="type" class="m-2" placeholder="Select">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">商品组合：</span>
        <ElInput v-model="eslName" class="w164" placeholder="请输入模板名称" />
      </div>
      <div class="condition">
        <span class="color_494949 ft12 pr5">彩屏模板：</span>
        <ElInput v-model="eslName" class="w164" placeholder="请输入模板名称" />
      </div>
      <div class="condition">
        <ElButton class="settingBtn" type="primary" @click="search">
          查询
        </ElButton>
      </div>
    </div>
    <div class="table">
      <ElTable
        ref="multipleTableRef"
        v-loading="loading"
        :border="true"
        :data="tableData"
      >
        <ElTableColumn label="内部编码" prop="ID" width="auto" />
        <ElTableColumn label="彩屏标识" prop="ESLCode" width="auto" />
        <ElTableColumn label="彩屏模板" prop="ESLName" width="auto" />
        <ElTableColumn label="屏幕类型" prop="ESLWidth" width="auto" />
        <ElTableColumn label="彩屏图片">
          <template #default="{ row }">
            <div class="img-container">
              <ElImage
                fit="contain"
                :hide-on-click-modal="true"
                :initial-index="9"
                :preview-src-list="[row.Image]"
                :src="row.Image"
                :z-index="99999"
                :zoom-rate="1.2"
              />
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="推送状态">
          <template #default="{ row }">
            <div>
              {{ row.Enabled == 1 ? '正常' : '未生效' }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="WIFI名称" prop="ESLWidth" width="auto" />
        <ElTableColumn label="显示方式" prop="ESLWidth" width="auto" />
        <ElTableColumn label="音量" prop="ESLWidth" width="auto" />
        <ElTableColumn label="内部码" prop="ESLWidth" width="auto" />
        <ElTableColumn label="服务地址" prop="ESLWidth" width="auto" />
        <ElTableColumn label="清除缓存方式" prop="ESLWidth" width="auto" />
        <ElTableColumn label="显示模式" prop="ESLWidth" width="auto" />
        <ElTableColumn label="程序升级" prop="ESLWidth" width="auto" />
        <ElTableColumn label="固件升级" prop="ESLWidth" width="auto" />
        <ElTableColumn label="创建时间" prop="ESLWidth" width="auto" />
        <ElTableColumn label="在线状态">
          <template #default="{ row }">
            <div>
              {{ row.Enabled == 1 ? '正常' : '未生效' }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作" width="200">
          <template #default="{ row }">
            <ElButton
              v-if="MenuAuth('edit')"
              link
              type="primary"
              @click="editFn(row)"
            >
              修改
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>
      <div v-if="total > 0" class="pagination">
        <ElPagination
          background
          :current-page="searchInfo.pageNo"
          :layout="layout"
          :page-size="searchInfo.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { MenuAuth } from '~/src/utils/permission'
  import { getPageData, deltemp } from '@/api/tag'
  import router from '@/router'
  import { useEleStore } from '@/store/modules/coms'
  const eslCode = ref('')
  const eslName = ref('')
  const searchInfo = reactive({
    pageNo: 1,
    pageSize: 20,
    tableName: '',
    sort: '',
    order: '',
    wheres: `[{"name":"ESLCode","value":${eslCode.value},"displayType":"like"}, {"name":"ESLName","value":${eslName.value},"displayType":"like"}]`,
    export: false,
    value: '',
  })
  const type: any = ref('')
  const types: any = ref([
    {
      label: '默认类型',
      value: '',
    },
  ])
  const loading = ref(false)
  const { clearWidgets, clearEditorData } = useEleStore()
  const addFn = async () => {
    await clearWidgets()
    await clearEditorData()
    router.push({ path: '/tag/editor' })
  }

  const layout = ref('total, sizes, prev, pager, next, jumper')
  const tableData = ref([])
  const total = ref(0)
  // 获取表格数据
  const gettableData = async () => {
    loading.value = true
    const { data } = await getPageData(searchInfo)
    loading.value = false
    // console.log(data)
    tableData.value = data?.rows || []
    total.value = data?.total || 0
  }
  gettableData()
  //分页条数切换
  const handleSizeChange = (val: any) => {
    searchInfo.pageSize = val
    gettableData()
  }
  //分页切换
  const handleCurrentChange = (val: any) => {
    searchInfo.pageNo = val
    gettableData()
  }

  const search = () => {
    searchInfo.pageNo = 1
    if (eslCode.value.trim() !== '' || eslName.value.trim() !== '') {
      const arr = []
      eslCode.value.trim() !== '' &&
        arr.push({ name: 'ESLCode', value: eslCode.value, displayType: 'like' })
      eslName.value.trim() !== '' &&
        arr.push({ name: 'ESLName', value: eslName.value, displayType: 'like' })
      searchInfo.wheres = JSON.stringify(arr)
    } else {
      searchInfo.wheres = `[{"name":"ESLCode","value": '',"displayType":"like"}, {"name":"ESLName","value": '',"displayType":"like"}]`
    }
    gettableData()
  }
  const editFn = (row: any) => {
    // router.push({ path: `/tag/editor`, query: { ID: row.ID } })
  }
  const $baseConfirm = inject('$baseConfirm') as any
  const deleteFn = async (row: any) => {
    $baseConfirm('你确定要删除当前项吗', null, async () => {
      await deltemp({
        id: row.ID,
      })
      gettableData()
    })
  }
</script>

<style lang="scss" scoped>
  .tpl-container {
    .color_494949 {
      color: #494949;
    }

    .noData {
      text-align: center;
      font-size: 16px;
      color: gray;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }

    .img-container {
      display: flex;
      // justify-content: center;

      .img {
        width: 100px;
      }
    }

    .condition-container {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      .condition {
        margin-right: 14px;
        margin-bottom: 10px;
      }
    }
  }
  .el-table {
    & ::v-deep th.el-table__cell,
    ::v-deep td.el-table__cell {
      // 设置position 使得 子元素不与其产生新的层叠关系
      position: static;
    }
  }
</style>
